package io.xxx.omni.osc.domain

import com.alibaba.fastjson.JSON
import java.time.LocalDateTime

/**
 * 平台
 */
data class Platform(
    val id: String,
    val name: String,
    val enabled: Boolean,
    val created: LocalDateTime,
    val modified: LocalDateTime,
    val authorizations: List<Authorization>,
) {
    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as Platform

        if (id != other.id) return false
        if (modified != other.modified) return false

        return true
    }

    override fun hashCode(): Int {
        var result = id.hashCode()
        result = 31 * result + this.modified.hashCode()
        return result
    }
}

/**
 * 店铺
 */
data class Store(
    val id: String,
    val vid: String?,
    val pid: String,
    /**
     * 平台商家id
     */
    val oid: String?,
    val name: String,
    val oname: String?,
    val enabled: Boolean,
    val created: LocalDateTime,
    val modified: LocalDateTime,
    val platform: Platform,
    val authorization: Authorization,
) {

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as Store

        if (id != other.id) return false
        if (modified != other.modified) return false

        return true
    }

    override fun hashCode(): Int {
        var result = id.hashCode()
        result = 31 * result + modified.hashCode()
        return result
    }
}

data class Authorization(
    val id: Int,
    val pid: String,
    val sid: String?,
    val data: String,
    val enabled: Boolean,
    val created: LocalDateTime,
    val modified: LocalDateTime,
) {
    operator fun get(key: String): String = dataJson.getString(key)

    private val dataJson by lazy {
        JSON.parseObject(data)
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (javaClass != other?.javaClass) return false

        other as Authorization

        if (id != other.id) return false
        if (modified != other.modified) return false

        return true
    }

    override fun hashCode(): Int {
        var result = id.hashCode()
        result = 31 * result + modified.hashCode()
        return result
    }

    operator fun compareTo(other: Authorization): Int {
        return modified.compareTo(other.modified)
    }
}